package com.ftg.learn.upload.service.impl;

import com.ftg.learn.common.result.Result;
import com.ftg.learn.common.utils.AliOssUtil;
import com.ftg.learn.upload.service.FileUploadService;
import com.ftg.learn.upload.utils.FileUploadUtil;
import com.ftg.learn.upload.vo.FileUploadVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

import static com.ftg.learn.common.constant.CodeConstant.INTERNAL_SERVER_ERROR;
import static com.ftg.learn.common.constant.CodeConstant.OK;
import static com.ftg.learn.common.constant.MessageErrorConstant.UPLOAD_ERROR;
import static com.ftg.learn.common.constant.MessageSuccessConstant.UPLOAD_SUCCESS;

@Slf4j
@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private AliOssUtil aliOssUtil;

    @Override
    public Result<FileUploadVO> uploadFile(MultipartFile file) {
        log.info("开始上传文件：{}", file.getOriginalFilename());
        
        // 验证文件
        String validateResult = FileUploadUtil.validateFile(file);
        if (validateResult != null) {
            return Result.build(INTERNAL_SERVER_ERROR, validateResult);
        }
        
        try {
            // 生成存储路径
            String objectName = FileUploadUtil.generateFilePath(file.getOriginalFilename());
            
            // 上传到OSS
            String url = aliOssUtil.upload(file.getBytes(), objectName);
            
            // 构建响应对象
            FileUploadVO uploadVO = new FileUploadVO();
            uploadVO.setUrl(url);
            uploadVO.setType(FileUploadUtil.getFileType(file.getOriginalFilename()));
            uploadVO.setSize(file.getSize());
            uploadVO.setOriginalFilename(file.getOriginalFilename());
            uploadVO.setFilename(objectName);
            
            log.info("文件上传成功：{}", url);
            return Result.build(OK, UPLOAD_SUCCESS, uploadVO);
        } catch (Exception e) {
            log.error("文件上传失败：{}", e.getMessage());
            return Result.build(INTERNAL_SERVER_ERROR, UPLOAD_ERROR);
        }
    }

    @Override
    public Result<List<FileUploadVO>> batchUploadFiles(MultipartFile[] files) {
        log.info("开始批量上传文件，文件数量：{}", files.length);
        
        List<FileUploadVO> uploadVOs = new ArrayList<>();
        List<String> errorFiles = new ArrayList<>();
        
        for (MultipartFile file : files) {
            // 验证文件
            String validateResult = FileUploadUtil.validateFile(file);
            if (validateResult != null) {
                errorFiles.add(file.getOriginalFilename() + ": " + validateResult);
                continue;
            }
            
            try {
                // 生成存储路径
                String objectName = FileUploadUtil.generateFilePath(file.getOriginalFilename());
                
                // 上传到OSS
                String url = aliOssUtil.upload(file.getBytes(), objectName);
                
                // 构建响应对象
                FileUploadVO uploadVO = new FileUploadVO();
                uploadVO.setUrl(url);
                uploadVO.setType(FileUploadUtil.getFileType(file.getOriginalFilename()));
                uploadVO.setSize(file.getSize());
                uploadVO.setOriginalFilename(file.getOriginalFilename());
                uploadVO.setFilename(objectName);
                
                uploadVOs.add(uploadVO);
                log.info("文件上传成功：{}", url);
            } catch (Exception e) {
                log.error("文件上传失败：{}", e.getMessage());
                errorFiles.add(file.getOriginalFilename() + ": " + e.getMessage());
            }
        }
        
        if (uploadVOs.isEmpty()) {
            return Result.build(INTERNAL_SERVER_ERROR, "所有文件上传失败：" + String.join(", ", errorFiles));
        }
        
        if (!errorFiles.isEmpty()) {
            return Result.build(OK, "部分文件上传成功，失败文件：" + String.join(", ", errorFiles), uploadVOs);
        }
        
        return Result.build(OK, UPLOAD_SUCCESS, uploadVOs);
    }
} 